| Conditions | 1 |
| Paths | 1 |
| Total Lines | 219 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function (state, data, visibility, util, format, reaction) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | ct.util = util; |
||
| 21 | ct.format = format; |
||
| 22 | ct.reaction = reaction; |
||
| 23 | |||
| 24 | ct.update = function(player) { |
||
| 25 | processRedox(player); |
||
| 26 | processElectronegativity(player); |
||
| 27 | }; |
||
| 28 | |||
| 29 | function processRedox(player){ |
||
| 30 | for(let slot of player.element_slots){ |
||
| 31 | if(!slot){ |
||
| 32 | continue; |
||
| 33 | } |
||
| 34 | for (let redox of slot.redoxes) { |
||
| 35 | if (!redox.resource || !redox.active || redox.from === redox.to) { |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | |||
| 39 | let reactant = ct.generateName(redox.element, redox.from); |
||
| 40 | let power = ct.redoxPower(player); |
||
| 41 | let number = Math.min(power, player.resources[reactant].number); |
||
| 42 | let react = ct.redoxReaction(redox); |
||
| 43 | |||
| 44 | ct.reaction.react(number, react, player); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | function processElectronegativity(player){ |
||
| 50 | for(let key in data.elements){ |
||
| 51 | let element = data.elements[key]; |
||
| 52 | if(element.electronegativity === 0){ |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | let ions = element.anions.concat(element.cations); |
||
| 56 | ions.push(element.main); |
||
| 57 | for(let resource of ions){ |
||
| 58 | if(player.resources[resource].number === 0){ |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | let charge = data.resources[resource].charge || 0; |
||
| 62 | let probabilities = probabilityDistribution(key, charge); |
||
| 63 | for(let probKey in probabilities){ |
||
| 64 | if(probKey === charge){ |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | let production = Math.floor(probabilities[probKey]*player.resources[resource].number); |
||
| 68 | if(production === 0){ |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | let react = ct.redoxReaction({ |
||
| 72 | element: key, |
||
| 73 | from: charge, |
||
| 74 | to: parseInt(probKey, 10) |
||
| 75 | }); |
||
| 76 | // electronegativity is 'for free' |
||
| 77 | react.reactant.eV = 0; |
||
| 78 | // FIXME: starvation should fix this |
||
| 79 | if(react.reactant['e-']){ |
||
| 80 | production = Math.min(production, player.resources['e-'].number); |
||
| 81 | } |
||
| 82 | ct.reaction.react(production, react, player); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | function probabilityDistribution(element, charge){ |
||
| 89 | let prob = {}; |
||
| 90 | let start = -data.elements[element].electron_affinity.length; |
||
| 91 | let end = charge; |
||
| 92 | // lower than index, affected by negativity |
||
| 93 | rangeProbability(element, prob, start, end, 1, data.elements[element].negative_factor); |
||
| 94 | |||
| 95 | prob[charge] = 1; |
||
| 96 | |||
| 97 | start = charge+1; |
||
| 98 | end = data.elements[element].ionization_energy.length+1; |
||
| 99 | // lower than index, affected by positivity |
||
| 100 | rangeProbability(element, prob, start, end, -1, data.elements[element].positive_factor); |
||
| 101 | |||
| 102 | let sum = 0; |
||
| 103 | for(let i in prob){ |
||
| 104 | sum += prob[i]; |
||
| 105 | } |
||
| 106 | for(let i in prob){ |
||
| 107 | prob[i] /= sum; |
||
| 108 | } |
||
| 109 | return prob; |
||
| 110 | } |
||
| 111 | |||
| 112 | function rangeProbability(element, prob, start, end, offset, factor){ |
||
| 113 | for(let i = start; i < end; i++){ |
||
| 114 | let difference = data.redox[element][i]-data.redox[element][i+offset]; |
||
| 115 | if(difference <= 0){ |
||
| 116 | difference = -difference; |
||
| 117 | }else{ |
||
| 118 | difference = 1/difference; |
||
| 119 | } |
||
| 120 | prob[i] = Math.pow(data.constants.ELECTRONEGATIVITY_CHANCE,Math.abs(i))*factor*difference; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /* Calculates the redox power based on the redox upgrades */ |
||
| 125 | ct.redoxPower = function(player) { |
||
| 126 | let level = player.global_upgrades.redox_bandwidth; |
||
| 127 | let upgrade = data.global_upgrades.redox_bandwidth; |
||
| 128 | let basePower = upgrade.power; |
||
| 129 | let polynomial = upgrade.power_poly; |
||
| 130 | return basePower * Math.floor(Math.pow(level, polynomial)); |
||
| 131 | }; |
||
| 132 | |||
| 133 | /* Writes a redox in the form of a reaction so that we can use the reaction |
||
| 134 | service to process it */ |
||
| 135 | ct.redoxReaction = function (redox) { |
||
| 136 | let reactant = ct.generateName(redox.element, redox.from); |
||
| 137 | let product = ct.generateName(redox.element, redox.to); |
||
| 138 | let energy = redoxEnergy(redox.from, redox.to, redox.element); |
||
| 139 | |||
| 140 | let react = { |
||
| 141 | 'reactant': {}, |
||
| 142 | 'product': {} |
||
| 143 | }; |
||
| 144 | |||
| 145 | react.reactant[reactant] = 1; |
||
| 146 | react.product[product] = 1; |
||
| 147 | if (energy > 0) { |
||
| 148 | react.reactant.eV = energy; |
||
| 149 | } else if (energy < 0) { |
||
| 150 | react.product.eV = -energy; |
||
| 151 | } |
||
| 152 | |||
| 153 | let electron = redox.from - redox.to; |
||
| 154 | if (electron > 0) { |
||
| 155 | react.reactant['e-'] = electron; |
||
| 156 | } else if (electron < 0) { |
||
| 157 | react.product['e-'] = -electron; |
||
| 158 | } |
||
| 159 | |||
| 160 | return react; |
||
| 161 | }; |
||
| 162 | |||
| 163 | /* Calculates how much energy it takes to go from a redox level to another |
||
| 164 | for a given element */ |
||
| 165 | function redoxEnergy(from, to, element) { |
||
| 166 | let energyFrom = data.redox[element][from]; |
||
| 167 | let energyTo = data.redox[element][to]; |
||
| 168 | let energy = energyTo - energyFrom; |
||
| 169 | |||
| 170 | return energy; |
||
| 171 | } |
||
| 172 | |||
| 173 | /* Generates the name of a ion, e.g. O3+ */ |
||
| 174 | ct.generateName = function (element, i) { |
||
| 175 | if (i === 0) { |
||
| 176 | return data.elements[element].main; |
||
| 177 | } |
||
| 178 | let postfix = ''; |
||
| 179 | if (Math.abs(i) > 1) { |
||
| 180 | postfix = Math.abs(i); |
||
| 181 | } |
||
| 182 | postfix += getSign(i); |
||
| 183 | let name = element + postfix; |
||
| 184 | return name; |
||
| 185 | }; |
||
| 186 | |||
| 187 | function getSign(number) { |
||
| 188 | return number > 0 ? '+' : '-'; |
||
| 189 | } |
||
| 190 | |||
| 191 | /* Calculates the number of redox slots based on the redox upgrades */ |
||
| 192 | ct.redoxSlots = function (player) { |
||
| 193 | let level = player.global_upgrades.redox_slots; |
||
| 194 | let upgrade = data.global_upgrades.redox_slots; |
||
| 195 | let basePower = upgrade.power; |
||
| 196 | let multiplier = upgrade.power_mult; |
||
| 197 | return basePower * Math.floor(multiplier * level); |
||
| 198 | }; |
||
| 199 | |||
| 200 | ct.redoxSize = function (player) { |
||
| 201 | let size = 0; |
||
| 202 | for(let slot of player.element_slots){ |
||
| 203 | if(!slot){ |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | size += slot.redoxes.length; |
||
| 207 | } |
||
| 208 | return size; |
||
| 209 | }; |
||
| 210 | |||
| 211 | /* Adds a new redox to the player list */ |
||
| 212 | ct.addRedox = function (player, slot) { |
||
| 213 | if(ct.redoxSize(player) >= ct.redoxSlots(player)){ |
||
| 214 | return; |
||
| 215 | } |
||
| 216 | slot.redoxes.push({ |
||
| 217 | resource: data.elements[slot.element].main, |
||
| 218 | active: false, |
||
| 219 | element: slot.element, |
||
| 220 | from: 0, |
||
| 221 | to: 1 |
||
| 222 | }); |
||
| 223 | }; |
||
| 224 | |||
| 225 | ct.removeRedox = function (slot, index) { |
||
| 226 | slot.redoxes.splice(index, 1); |
||
| 227 | }; |
||
| 228 | |||
| 229 | ct.visibleRedox = function(slot) { |
||
| 230 | return slot.redoxes; |
||
| 231 | }; |
||
| 232 | |||
| 233 | state.registerUpdate('redox', ct.update); |
||
| 234 | } |
||
| 235 | ]); |
||
| 236 |